/**
 * 
 */
package com.app.mvc.action.rules;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.app.service.ICompetenceService;
import com.app.service.IRoleService;
import com.app.service.IRuleCService;
import com.app.service.vo.RoleVo;
import com.app.util.ReuqestToVo;

/**
 * @author yuxk
 *
 */
@Controller
@RequestMapping(value="/rule")
public class RoleAction {

	/**
	 * 
	 */
	public RoleAction() {
		// TODO Auto-generated constructor stub
	}
	@Autowired
	private IRoleService ruleservice;
	
	@Autowired
	private ICompetenceService competenceservie;
	@Autowired
	private IRuleCService rcservice;
	
	private static Logger logger = Logger.getLogger(RoleAction.class);
	
	/**
	 * 获取所有的角色信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/manageRule.form")
	public String getAllRole(HttpServletRequest request)
	{
		logger.info("展现所有角色信息");
		request.setAttribute("role-list", this.ruleservice.getAllRole());
		return "logins/admin/main-role";
	}
	
	/**
	 * 增加用户
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/rulesave.form")
	public String saveRole(HttpServletRequest request)
	{
		logger.info("增加角色信息");
		RoleVo vo = new RoleVo();
		ReuqestToVo.getVoFromRequest(request, vo);
		vo.setSign("0");
		this.ruleservice.saveRole(vo);
		return "forward:/rule/manageRule.form";
	}
	
	/**
	 * 展现角色具体信息
	 * @param roleid
	 * @return
	 */
	@RequestMapping(value="/showrule.form")
	public String showRole(@RequestParam("roleid") String roleid,HttpServletRequest request)
	{
		logger.info("显示角色详细信息");
		request.setAttribute("rulevo", this.ruleservice.getRoleVoById(roleid));
		return "logins/admin/show-rule";
	}
	
	/**
	 * 更新角色信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/editrule.form")
	public String updateRole(HttpServletRequest request)
	{
		RoleVo vo =  new RoleVo();
		ReuqestToVo.getVoFromRequest(request, vo);
		this.ruleservice.updateRole(vo);
		return "forward:/rule/showrule.form?roleid="+vo.getId();
	}
	
	/**
	 * 删除角色信息
	 * @param ruleid
	 * @return
	 */
	@RequestMapping(value="/deleterule.form")
	public String deleteRole(@RequestParam("ruleid") String ruleid)
	{
		RoleVo vo = new RoleVo();
		vo.setId(ruleid);
		this.ruleservice.deleteRole(vo);
		return "forward:/rule/manageRule.form";
	}
	
	/**
	 * 展现角色绑定权限页面
	 * @return
	 */
	@RequestMapping(value="/showbing.form")
	public String showbing(HttpServletRequest request)
	{
		request.setAttribute("rule-list", this.ruleservice.getAllRole());
		request.setAttribute("competence-list", this.competenceservie.getAllCornCompetenceVo());
		return "/logins/admin/rulecompentence/rule-comptence";
	}

	/**
	 * 角色绑定权限
	 * @param ruleid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/banging.form")
	public String bing(@RequestParam("ruleid") String ruleid,HttpServletRequest request)
	{
		logger.info("角色绑定权限");
		
		String[] completenceids = request.getParameterValues("compenteid");
		this.rcservice.addComptence(completenceids, ruleid);
		
		return this.showbing(request);
	}
	public IRoleService getRuleservice() {
		return ruleservice;
	}

	public void setRuleservice(IRoleService ruleservice) {
		this.ruleservice = ruleservice;
	}

	public ICompetenceService getCompetenceservie() {
		return competenceservie;
	}

	public void setCompetenceservie(ICompetenceService competenceservie) {
		this.competenceservie = competenceservie;
	}

	public IRuleCService getRcservice() {
		return rcservice;
	}

	public void setRcservice(IRuleCService rcservice) {
		this.rcservice = rcservice;
	}
	
	
	
}
